﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Configuration;
using System.IO;
using System.Reflection;
using LoaderLib;
using ExaminationTT.Control;
using ExaminationTT.Rule;
using ExaminationTT.Algorithms;
using OutputGenerator;


namespace ExaminationTT.View
{
    public partial class MainForm : Form
    {
        private ToolStripItem[] _toolLoadersItems;
        private ToolStripItem[] _toolAlgorithmsItems;
        private ToolStripItem[] _toolOutputGeneratorsItems;
        private HostApplication _host;
        private List<IRule> _rules;
        private List<ILoader> _loaders;
        private List<ViewRule> _viewRules;
        private List<IAlgorithm> _algorithms;
        private List<IOutputGenerator> _outputGenerator;
        public MainForm(HostApplication host)
        {
            _host = host;
            InitializeComponent();
            _rules = _host.GetRules();
            _loaders = _host.GetLoaders();
            _algorithms = _host.GetAlgorithms();
            _outputGenerator = _host.GetOutputGenerator();
            _viewRules = new List<ViewRule>();
            foreach (IRule rule in _rules)
            {
                _viewRules.Add(new ViewRule(rule));
            }
            BuildLoadersToolStrip();
            BuildAlgorithmsToolStrip();
            BuildOutputGeneratorToolStrip();
        }

        public void SetStatus(string msg)
        {
            toolStripStatusLabel1.Text = msg;
        }


        public string GetStatus()
        {
            return toolStripStatusLabel1.Text;
        }
    
        private void marcarExamesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Ver padrão singleton;
            Board boardView = new Board(_host, _viewRules);
            
            boardView.MdiParent= this;
            boardView.Show();
        }

        private void manageExamSeasonsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<ExamSeason> seasons = new List<ExamSeason>();
                UnitOfWork.Current.Get<int, ExamSeason>(typeof(ExamSeason)).LoadAllInto(seasons);
            SeasonsExams seForm = new SeasonsExams(_host, seasons);
            seForm.MdiParent = this;
            seForm.Show();

        }

        private void gerirRegrasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_rules.Count == 0)
            {
                MessageBox.Show("Não existem regras para gerir", this.Text);
                return;
            }
            
            RulesSetter rs = new RulesSetter(_viewRules, _host);
            rs.MdiParent = this;
            rs.Show();
            
        }

        private void BuildLoadersToolStrip()
        {
            _toolLoadersItems = new ToolStripItem[_loaders.Count];
            int size = 0;
            for (int i = 0; i < _loaders.Count; i++)
            {
                ILoader l = _loaders[i];
                _toolLoadersItems[i] = new ToolStripButton(l.Getname(), null, (sender, e) =>
                {
                    try
                    {
                        l.DataSource();
                    }
                    catch (InvalidOperationException ex)
                    {
                        //TODO Show Info about error
                        return;
                    }
                    SplashScreen.ShowSplashScreen();
                    _host.SetLoader(l);
                    _host.Setup();
                    SplashScreen.CloseForm();
                    size = size < l.Getname().Length ? l.Getname().Length : size;

                }
                );
                _toolLoadersItems[i].Size = new Size(100, 100);
                _tsm_loaders.DropDownItems.Add(_toolLoadersItems[i]);
            }
        }

        private void BuildOutputGeneratorToolStrip()
        {
            _toolOutputGeneratorsItems = new ToolStripItem[_outputGenerator.Count];
            int size = 0;
            for (int i = 0; i < _outputGenerator.Count; i++)
            {
                IOutputGenerator l = _outputGenerator[i];
                _toolOutputGeneratorsItems[i] = new ToolStripButton(l.Getname(), null, (sender, e) =>
                {
                    try
                    {
                        SplashScreen.ShowSplashScreen();
                        l.Setdata(_host.ExportScheduledExams());
                        l.GenerateOutPut();
                        SplashScreen.CloseForm();
                    }
                    catch (InvalidOperationException ex)
                    {
                        return;
                    }
                    size = size < l.Getname().Length ? l.Getname().Length : size;

                }
                );
                _toolOutputGeneratorsItems[i].Size = new Size(100, 100);
                exportToolStripMenuItem.DropDownItems.Add(_toolOutputGeneratorsItems[i]);
            }
            exportToolStripMenuItem.Visible = true;
        }

        private void BuildAlgorithmsToolStrip()
        {
            if (_algorithms.Count == 0) return;
            _toolAlgorithmsItems = new ToolStripItem[_algorithms.Count];
            //int size = 0;
            for (int i = 0; i < _algorithms.Count; i++)
            {
                IAlgorithm a = _algorithms[i];
                _toolAlgorithmsItems[i] = new ToolStripButton(a.Description, null, (sender, e) =>
                {
                    AlgorithmSetupForm algSetupForm = new AlgorithmSetupForm();
                    try
                    {
                        DialogResult res = algSetupForm.ShowDialog();
                        if (res == DialogResult.OK)
                        {
                            SplashScreen.ShowSplashScreen();
                            _host.RunAlgorithm(a, algSetupForm.WeekDays, algSetupForm.Hours);
                            SplashScreen.CloseForm();
                        }
                        else if(res == DialogResult.No)
                        {
                            SplashScreen.ShowSplashScreen();
                            _host.RunAlgorithm(a, null, null);
                            SplashScreen.CloseForm();
                        }
                        Board boardView = new Board(_host, _viewRules);
                        boardView.MdiParent = this;
                        boardView.Show();
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    //if (algSetupForm.ShowDialog() == DialogResult.OK)
                    //{
                    //    a.SetPossibleWeekDays(algSetupForm.WeekDays);
                    //    a.SetPossibleHours(algSetupForm.Hours);
                    //    _host.RunAlgorithm(a, algSetupForm.WeekDays, algSetupForm.Hours);
                    //}
                    //else 
                    //{
                    //    _host.RunAlgorithm(a, null, null);
                    //}
                    ////TODO set data
                    //a.SetCUnits(MapperControl.GetAllCUnit());
                    //List<IRule> rules = new List<IRule>();
                    //foreach (ViewRule vr in _viewRules)
                    //{
                    //    if (vr.Active) rules.Add(vr.Rule);
                    //}
                    //a.SetRules(rules);//ver se sao as regras activas
                    //a.SetSemesters(MapperControl.GetAllSemesters());
                    //a.SetSeasons(_host.GetSeasons());
                    //a.SetScheduledExams(_host.GetScheduledExams().Keys);
                    //a.SetUnScheduledExams(_host.GetUnscheduledExams());
                    ////GET SCHEDULED EXAMS
                    ////GET UNSCHEDULED EXAMS
                    //try
                    //{
                    //    a.Run();
                    //}
                    //catch (InvalidOperationException ex)
                    //{

                    //    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}
                    
                    
                }
                );
                _toolAlgorithmsItems[i].Size = new Size(100, 100);
                _algorithmsToolStripMenuItem.DropDownItems.Add(_toolAlgorithmsItems[i]);
            }
            _algorithmsToolStripMenuItem.Visible = true;
        }

        private void gravarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _host.SaveAllState();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _host.SaveAllState();
        }
    }
}
